﻿#region << 版 本 注 释 >>
/*----------------------------------------------------------------
// Copyright (C) 2022 kingerrobot
// 版权所有。 
//
// 文件名：BBakWmsOutOrderMaster
// 文件功能描述：
//
// 创建者：名字 (zzl)
// 时间：2022/1/21 14:33:47
//
// 修改人：
// 时间：
// 修改说明：
// 版本：V1.0.0
//----------------------------------------------------------------*/
#endregion
using KingerRobot.MexicoSailunMDC.Dao;

namespace KingerRobot.MexicoSailunMDC.SpiService;

internal class BBakWmsOutOrderMaster : IBakWmsOutOrderMaster
{
    private readonly static IBakWmsOutOrderSummary _BakWmsOutOrderSummary = new BBakWmsOutOrderSummary();
    public POBakWmsOutOrderMaster GetBakWmsOutOrderMasterById(long id)
    {
        var db = Db.Default;
        var result = db.BakWmsOutOrderMasters.Where(t => t.Id == id).FirstOrDefault();
        return result;
    }



    public int DeleteBakOrderByDateTime(DateTime dateTime)
    {
        var db = Db.Default;
        var bakOrders = db.BakWmsOutOrderMasters
            .Where(t => t.BackTime < dateTime)
            .OrderBy(t => t.BackTime)
            .Take(10)
            .ToList();

        var orderIds = bakOrders.Select(t => t.Id).ToList();

        var result = DeleteBakOrderAndCmd(orderIds);

        return result;
    }

    private int DeleteBakOrderAndCmd(List<long?> orderIds)
    {
        var result = DeleteBakOrder(orderIds);

        result += _BakWmsOutOrderSummary.DeleteBakOutOrderSummary(orderIds);

        return result;
    }

    private int DeleteBakOrder(List<long?> orderIds)
    {
        var db = Db.Default;
        var result = db.BakWmsOutOrderMasters.Where(t => orderIds.Contains(t.Id)).Delete();
        return result;
    }

}